package com.hzwq.Utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.annotation.RequiresApi;

import java.lang.reflect.Method;

import static com.hzwq.base.MyApplication.mInstance;

/**
 * 描 述：
 * 作 者：kanglanlan  2021/3/29 10:02
 * 修改描述： XXX
 * 修 改 人： XXX  2021/3/29 10:02
 * 修改版本： XXX
 */
public class OpenHotSpot {

    @SuppressLint("MissingPermission")
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static void open() {
        WifiManager manager = (WifiManager) mInstance.getSystemService(Context.WIFI_SERVICE);
        manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
            @Override
            public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
                super.onStarted(reservation);
                Log.d("TAG", "Wifi Hotspot is on now");
            }

            @Override
            public void onStopped() {
                super.onStopped();
                Log.d("TAG", "onStopped: ");
            }

            @Override
            public void onFailed(int reason) {
                super.onFailed(reason);
                Log.d("TAG", "onFailed: ");
            }
        }, new Handler());

    }

    /**
     * 8.0 开启wifi热点方法
     *
     * @param apName     wifi热点帐号
     * @param apPassword wifi热点密码
     */
    public static boolean setWifiAp(String apName, String apPassword) {

        try {
            WifiManager mWifiManager = (WifiManager) mInstance.getSystemService(Context.WIFI_SERVICE);

            //热点的配置类
            WifiConfiguration apConfig = new WifiConfiguration();
            //配置热点的名称(可以在名字后面加点随机数什么的)
            apConfig.SSID = apName;
            apConfig.preSharedKey = apPassword;
            //不设置密码
            apConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            //android 8.0开启wifi热点
            Method method;
            if (Build.VERSION.SDK_INT >= 26) {
                Method configMethod = mWifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);
                boolean isConfigured = (Boolean) configMethod.invoke(mWifiManager, apConfig);

                method = mWifiManager.getClass().getMethod("startSoftAp", WifiConfiguration.class);
                //返回热点打开状态
                return true;
//                return (Boolean) method.invoke(mWifiManager, apConfig);
            } else {
                //通过反射调用设置热点
                method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
                //返回热点打开状态
                return (Boolean) method.invoke(mWifiManager, apConfig, true);
            }
        } catch (Exception e) {
            e.getMessage();
            return false;
        }
    }

    /**
     * 关闭wifi 热点
     *
     * @return
     */
    @SuppressLint("SoonBlockedPrivateApi")
    public boolean closeWifiAp() {
        //热点的配置类
        WifiManager mWifiManager = (WifiManager) mInstance.getSystemService(Context.WIFI_SERVICE);

        WifiConfiguration apConfig = new WifiConfiguration();
        try {
            //android 8.0关闭wifi热点
            Method method;
            if (Build.VERSION.SDK_INT >= 26) {
                method = mWifiManager.getClass().getDeclaredMethod("stopSoftAp");
                return (Boolean) method.invoke(mWifiManager);
            } else {
                method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
                //返回热点关闭状态
                return (Boolean) method.invoke(mWifiManager, apConfig, false);
            }
        } catch (Exception e) {
            e.getMessage();
            return false;
        }

    }

    /**
     * 打开移动网络
     *
     * @param context
     * @param enabled 是否打开
     */
    public void setMobileDataState(Context context, boolean enabled) {
        TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        try {
            Method setDataEnabled = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
            if (null != setDataEnabled) {
                setDataEnabled.invoke(telephonyService, enabled);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 打开手机热点
     */
    public static void openHot() {
        WifiManager wifiManager = (WifiManager) mInstance.getSystemService(Context.WIFI_SERVICE);
        //这里先说明一下在6.0及其以下版本，在开启热点之前要先手动关闭wifi。以后版本就不需要了会自动关闭，热点关闭后也会自动打开
        if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
            if (wifiManager.isWifiEnabled()) { //如果wifi打开关闭wifi
                wifiManager.setWifiEnabled(false);
            }
        }
//        第一种：7.1及其以下版本开启关闭热点
//        if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1) {
        try {
            WifiConfiguration apConfig = new WifiConfiguration();
            //配置热点的名称
            apConfig.SSID = "HZWQHOT";
            //配置热点的密码(至少8位)
            apConfig.preSharedKey = "12345678";
            apConfig.allowedKeyManagement.set(4);
            //通过反射调用设置热点
            Method method = wifiManager.getClass().getMethod(
                    "setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
            Boolean rs = (Boolean) method.invoke(wifiManager, apConfig, true);//true开启热点 false关闭热点
            Log.d("-------", "开启是否成功:" + rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        }
//        第二种：8.0版本开启热点。这种方法不用我们去设置账号密码，系统会随机产生，并且在回调方法中可以直接拿到。这里需要注意的是要在远程服务中去开启，否则会出现退出到后台热点关闭的情况。

    }
}
